package xyz.xtt.activity.service.impl;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toMap;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;

import xyz.xtt.activity.entity.TblArtAmberCard;
import xyz.xtt.activity.mapper.TblArtAmberCardMapper;
import xyz.xtt.activity.response.ArtAmberCardResponse;
import xyz.xtt.activity.service.ITblArtAmberCardService;

/**
 * <p>
 * 卡片明细表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-19
 */
@Service
public class TblArtAmberCardServiceImpl extends ServiceImpl<TblArtAmberCardMapper, TblArtAmberCard> implements ITblArtAmberCardService {
	/**
	 * 详情列表
	 * 
	 * @param appUid
	 * @return
	 */
	private List<ArtAmberCardResponse> info(Long appUid) {
		List<TblArtAmberCard> queryList = new LambdaQueryChainWrapper<>(baseMapper).eq(TblArtAmberCard::getAppUserId, appUid).list();
		Map<String, Long> groupCount = queryList.stream().collect(groupingBy(TblArtAmberCard::getRarityType, Collectors.counting()));
		Map<String, TblArtAmberCard> toMap = queryList.stream().collect(toMap(TblArtAmberCard::getRarityType, Function.identity(), (a, b) -> a));
		List<ArtAmberCardResponse> resultList = Lists.newArrayList();
		toMap.forEach((k, v) -> {
			ArtAmberCardResponse build = ArtAmberCardResponse.builder().amount(groupCount.get(k)).imgUrl(v.getProfileImg()).build();
			resultList.add(build);
		});
		return resultList;
	}

	@Override
	public Long queryCardSize(Long amberOrderId) {
		Long cardSize = new LambdaQueryChainWrapper<>(baseMapper).eq(TblArtAmberCard::getAmberOrderId, amberOrderId).count();
		return cardSize;
	}

}
